home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / PRTPerVertex / SHIrradianceEnvMap.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  3.9 KB  |  141 lines

  1. //-----------------------------------------------------------------------------
  2. // File: SHIrradianceEnvMap.fx
  3. //
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6.  
  7.  
  8. //-----------------------------------------------------------------------------
  9. // Global variables
  10. //-----------------------------------------------------------------------------
  11. float4x4 g_mWorldViewProjection;
  12. texture  AlbedoTexture;
  13.  
  14. float4 MaterialDiffuseColor = { 1.0f, 1.0f, 1.0f, 1.0f };    
  15.  
  16. float4 cAr;
  17. float4 cAg;
  18. float4 cAb;
  19. float4 cBr;
  20. float4 cBg;
  21. float4 cBb;
  22. float4 cC;
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. // Texture samplers
  27. //-----------------------------------------------------------------------------
  28. sampler AlbedoSampler = 
  29. sampler_state
  30. {
  31.     Texture = <AlbedoTexture>;
  32.     MipFilter = LINEAR;
  33.     MinFilter = LINEAR;
  34.     MagFilter = LINEAR;
  35. };
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Vertex shader output structure
  40. //-----------------------------------------------------------------------------
  41. struct VS_OUTPUT
  42. {
  43.     float4 Position  : POSITION;    // position of the vertex
  44.     float4 Diffuse   : COLOR0;      // diffuse color of the vertex
  45.     float2 TextureUV : TEXCOORD0;   // typical texture coords stored here
  46. };
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. VS_OUTPUT SHIrradianceEnvironmentMapVS( float4 vPos : POSITION, 
  51.                                         float4 vNormal : NORMAL,
  52.                                         float2 vTexCoord0 : TEXCOORD0,
  53.                                         uniform bool bTexture )
  54. {
  55.     VS_OUTPUT Output;
  56.     
  57.     // Output the vetrex position in projection space
  58.     Output.Position = mul(vPos, g_mWorldViewProjection);
  59.     
  60.     float3 x1, x2, x3;
  61.     
  62.     // Linear + constant polynomial terms
  63.     x1.r = dot(cAr,vNormal);
  64.     x1.g = dot(cAg,vNormal);
  65.     x1.b = dot(cAb,vNormal);
  66.     
  67.     // 4 of the quadratic polynomials
  68.     float4 vB = vNormal.xyzz * vNormal.yzzx;   
  69.     x2.r = dot(cBr,vB);
  70.     x2.g = dot(cBg,vB);
  71.     x2.b = dot(cBb,vB);
  72.    
  73.     // Final quadratic polynomial
  74.     float vC = vNormal.x*vNormal.x - vNormal.y*vNormal.y;
  75.     x3 = cC.rgb * vC;    
  76.  
  77.     Output.Diffuse.rgb = x1 + x2 + x3;
  78.     Output.Diffuse.a   = 1.0f; 
  79.     
  80.     Output.Diffuse *= MaterialDiffuseColor;
  81.     
  82.     if( bTexture )
  83.         Output.TextureUV = vTexCoord0;
  84.     else
  85.         Output.TextureUV = 0;
  86.     
  87.     return Output;
  88. }
  89.  
  90.  
  91. //-----------------------------------------------------------------------------
  92. // Pixel shader output structure
  93. //-----------------------------------------------------------------------------
  94. struct PS_OUTPUT
  95. {   
  96.     float4 RGBColor : COLOR0;  // Pixel color    
  97. };
  98.  
  99.  
  100. //-----------------------------------------------------------------------------
  101. // Trival pixel shader (could use FF if desired)
  102. //-----------------------------------------------------------------------------
  103. PS_OUTPUT StandardPS( VS_OUTPUT In,
  104.                       uniform bool bTexture ) 
  105.     PS_OUTPUT Output;
  106.  
  107.     if( bTexture )
  108.         Output.RGBColor = tex2D(AlbedoSampler, In.TextureUV) * In.Diffuse;
  109.     else
  110.         Output.RGBColor = In.Diffuse;
  111.  
  112.     return Output;
  113. }
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. technique RenderWithSHIrradEnvMap
  118. {
  119.     pass P0
  120.     {          
  121.         VertexShader = compile vs_1_1 SHIrradianceEnvironmentMapVS( true );
  122.         PixelShader  = compile ps_1_1 StandardPS( true ); 
  123.     }
  124. }
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. technique RenderWithSHIrradEnvMapNoAlbedo
  129. {
  130.     pass P0
  131.     {          
  132.         VertexShader = compile vs_1_1 SHIrradianceEnvironmentMapVS( false );
  133.         PixelShader  = compile ps_1_1 StandardPS( false );
  134.     }
  135. }
  136.  
  137.  
  138.  
  139.  
  140.